<?php

namespace App\Admin\Controllers;

use App\Models\Orders;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '订单列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Orders);
        $grid->model()->orderBy('id','desc');

        $grid->column('id', __('Id'));
        $grid->column('order_code', __('订单编号'));
        $grid->column('user_id', __('用户ID'));
        $grid->column('user.mobile', __('用户手机号'));
        $grid->column('goods_type', __('商品类型'))->display(function($value) {
            return Orders::$typeLabels[$value];
        });
        $grid->column('goods_id', __('商品ID'));
        $grid->column('title', __('标题'));
        $grid->column('desc', __('描述'));
        $grid->column('price', __('订单金额（分）'));
        $grid->column('status', __('订单状态'))->display(function($value) {
            return Orders::$statusLabels[$value];
        });
        $grid->column('pay_type', __('支付类型'))->display(function($value) {
            return Orders::$payTypeLabels[$value];
        });
        $grid->column('prepay_id', __('预支付ID'));
        $grid->column('logs', __('日志'));
        $grid->column('thirty_order', __('三方订单号'));
        $grid->column('pay_time', __('支付时间'));
        $grid->column('date', __('日期'));
//        $grid->column('refund_price', __('Refund price'));
//        $grid->column('out_refund_no', __('Out refund no'));
//        $grid->column('refund_logs', __('Refund logs'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->filter(function ($filter){
            //$filter->disableIdFilter();
//            $filter->where(function ($query, $value) {
//                if ($value) {
//                    $query->whereHas('user', function ($query) use ($value) {
//                        $query->where('mobile', 'like', "%{$value}%");
//                    });
//                }
//            }, '手机号');
//            $filter->where(function ($query) {
//                $query->whereHas('user', function ($query) {
//                    $query->where('mobile', 'like', "%{$this->input}%");
//                });
//
//            }, '手机号');


            $filter->equal('status', '支付状态')->radio(Orders::$statusLabels);
            $filter->equal('goods_type', '订单类型')->radio(Orders::$typeLabels);
            $filter->like('thirty_order', '三方订单号');
        });
//        $grid->actions(function (Grid\Displayers\Actions $actions){
//            $actions->append('<a href="/admin/user/clear-record?id=' . $actions->getKey()
//                             . '">清空学习记录</a>&nbsp;&nbsp;&nbsp;&nbsp;');
//        });



        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Orders::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('order_code', __('Order code'));
        $show->field('user_id', __('User id'));
        $show->field('goods_type', __('Goods type'));
        $show->field('goods_id', __('Goods id'));
        $show->field('title', __('Title'));
        $show->field('desc', __('Desc'));
        $show->field('price', __('Price'));
        $show->field('status', __('Status'));
        $show->field('pay_type', __('Pay type'));
        $show->field('prepay_id', __('Prepay id'));
        $show->field('logs', __('Logs'));
        $show->field('thirty_order', __('Thirty order'));
        $show->field('pay_time', __('Pay time'));
        $show->field('refund_price', __('Refund price'));
        $show->field('out_refund_no', __('Out refund no'));
        $show->field('refund_logs', __('Refund logs'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Orders);

        $form->text('order_code', __('Order code'));
        $form->number('user_id', __('User id'));
        $form->number('goods_type', __('Goods type'));
        $form->number('goods_id', __('Goods id'));
        $form->text('title', __('Title'));
        $form->text('desc', __('Desc'));
        $form->number('price', __('Price'));
        $form->number('status', __('Status'));
        $form->number('pay_type', __('Pay type'))->default(1);
        $form->text('prepay_id', __('Prepay id'));
        $form->textarea('logs', __('Logs'));
        $form->text('thirty_order', __('Thirty order'));
        $form->number('pay_time', __('Pay time'));
        $form->number('refund_price', __('Refund price'));
        $form->text('out_refund_no', __('Out refund no'));
        $form->textarea('refund_logs', __('Refund logs'));

        return $form;
    }
}
